package com.winkey.facility.gateway.restapi;

import com.sun.jna.NativeLong;
import com.winkey.facility.gateway.connector.gate.parking.ParkingGateConnector;
import com.winkey.facility.gateway.service.face.FaceFacilityService;
import com.winkey.facility.gateway.service.parking.ParkingFacilityService;
import com.winkey.facility.gateway.service.utils.DateArraysFormatUtil;
import com.winkey.facility.gateway.service.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


/**
 * @author yiyingzai
 */
@RestController
@RequestMapping("/face/parking")
public class FacilityController {
    private ParkingFacilityService parkingFacilityService;
    private FaceFacilityService faceFacilityService;
    private ParkingGateConnector parkingGateConnector;
    @Autowired
    public FacilityController(ParkingFacilityService parkingFacilityService,FaceFacilityService faceFacilityService,ParkingGateConnector parkingGateConnector){
        this.parkingFacilityService = parkingFacilityService;
        this.faceFacilityService = faceFacilityService;
        this.parkingGateConnector = parkingGateConnector;
    }
    @PostMapping("/addFaceDevice")
    public String save(String deviceNo, String name, String tag){
        return faceFacilityService.save(deviceNo,name,tag);
    }
    @PostMapping("/remove")
    public String remove(String deviceNo){
        return faceFacilityService.remove(deviceNo);
    }
    @GetMapping("/page")
    public String page(int size){
        return faceFacilityService.page(size);
    }
    @GetMapping("/single")
    public String single(String deviceNo){
        return faceFacilityService.single(deviceNo);
    }
    @PostMapping("/enable")
    public String enable(String deviceNo){
        return faceFacilityService.enable(deviceNo);
    }
    @PostMapping("/disable")
    public String disable(String deviceNo){
        return faceFacilityService.disable(deviceNo);
    }
    @PostMapping("/remoteOpenDoor")
    public String remoteOpenDoor(String deviceNo){
        return faceFacilityService.remoteOpenDoor(deviceNo);
    }
    @PostMapping("/setOpenDoorCondition")
    public String setOpenDoorCondition(String deviceNo,String openDoorWay){
        return faceFacilityService.setOpenDoorCondition(deviceNo,openDoorWay);
    }
    @PostMapping("/authPersonPage")
    public String authPersonPage(){
        return faceFacilityService.authPersionPage();
    }
    @PostMapping("/singlePersonAuth")
    public String singlePersonAuth(){
        return faceFacilityService.singlePersonAuth();
    }
    @PostMapping("/singlePersonRevoke")
    public String singlePersonRevoke(){
        return faceFacilityService.singlePersonRevoke();
    }
    @PostMapping("/batchAuth")
    public String batchAuth(){
        return faceFacilityService.batchAuth();
    }
    @PostMapping("/modifyAuth")
    public String modifyAuth(){
        return faceFacilityService.modifyAuth();
    }
    @GetMapping("/findDeviceOnlineStatus")
    public String findDeviceOnlineStatus(String deviceNo){
        return faceFacilityService.findDeviceOnlineStatus(deviceNo);
    }
    @GetMapping("/refreshStatus")
    public String refreshStatus(String deviceNos){
        return faceFacilityService.refeshStatus(deviceNos);
    }


    @PostMapping("/openGate")
    public R openGate(String ip){
        return R.ok(parkingFacilityService.openGate(ip) == 1? 200 : 0,"openGate success");
    }

    @PostMapping("/closeGate")
    public R closeGate(String ip){
        return R.ok(parkingFacilityService.closeGate(ip) == 1? 200 : 0,"closeGate success");
    }

    @PostMapping("/setWorkMode")
    public R setWorkMode(String ip,int mode){
        //mode  1:入口 2:出口
        NativeLong nid = parkingGateConnector.login(ip);
        return R.ok(parkingFacilityService.setWorkMode(nid,(byte)mode) == 1? 200 : 0,"setWorkMode success");
    }

    @PostMapping("/setParkVip")
    public R setParkVip(String ip,String plate,int type,String startTime,String endTime){

        try {
            NativeLong nid = parkingGateConnector.login(ip);
            int[] start = DateArraysFormatUtil.formatDate(startTime);
            int[] end = DateArraysFormatUtil.formatDate(endTime);
            int code = parkingFacilityService.setParkVip(nid,plate.getBytes("GBK"),(byte)type,start,end);
            return R.ok(code == 1? 200 : 0,"setParkVip success");
        } catch (Exception e) {
                e.printStackTrace();
                return null;
            }

    }

    @PostMapping("/logout")
    public R logout(String loginid){
        NativeLong nid = new NativeLong();
        nid.setValue(Long.parseLong(loginid));
        return R.ok(loginid+","+parkingGateConnector.logout(nid));
    }
}
